package com.jiu.core;

import java.awt.Color;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Vector;
import java.util.concurrent.ConcurrentHashMap;
import javax.swing.JOptionPane;
import org.apache.log4j.Logger;
import com.jiu.bean.FishThread;
import com.jiu.bean.QQThread;
import com.jiu.bean.SendMessageThread;
import com.jiu.config.Config;
import com.jiu.http.fish.FishHttpService;
import com.jiu.ui.LoginFrame;
import com.jiu.ui.MainFrame;

public class FishClient implements FishContext{
	private Config config;
	private Account account;
	private LoginFrame loginFrame;
	private MainFrame mainFrame;
	private FishHttpService fishHttpService;
	private FishThread fishThread;
	private SendMessageThread sendThread;	
	private QQThread qqThread;
	private Map<Integer, String> sendMap = new ConcurrentHashMap<Integer, String>();// 钓鱼将发出的信息分，1，2，3，4，5，6.。每次发送最大key对应的值
	private Logger log = Logger.getLogger(FishClient.class);
	public FishClient(){
		this.config = new Config();//配置
		this.account = new Account();
		this.loginFrame = new LoginFrame(this);
		this.mainFrame = new MainFrame(this);//主窗口
		this.fishThread = new FishThread(this);
		this.sendThread = new SendMessageThread(this);
		this.qqThread = new QQThread(this);
		this.fishHttpService = new FishHttpService(this);
		init();//初始化
	}
	public void init(){
		Vector<Account> accounts = getConfig().getAccounts();
		for(Account account:accounts){
			loginFrame.addJComboxItem(account.getQq());			
		}
		loginFrame.setVisible(true);
	}
	@Override
	public Config getConfig() {
		return config;
	}
	@Override
	public Account getAccount() {
		return account;
	}
	@Override
	public LoginFrame getLoginFrame() {
		return loginFrame;
	}
	@Override
	public MainFrame getMainFrame() {
		return mainFrame;
	}
	@Override
	public void setAccount(Account account) {
		this.account = account;
	}
	@Override
	public FishHttpService getFishHttpService() {
		return fishHttpService;
	}
	@Override
	public void updateStatus(boolean status) {		
		if(account.getGroupid()==null){
			mainFrame.updateStateBox(true);
			JOptionPane.showMessageDialog(mainFrame, "未捕获到钓鱼客户端密码串，请先登录客户端！");
		}else{
			account.setStop(status);
			mainFrame.updateStateBox(status);
			if(status){
				//离开
				fishHttpService.LogOut();
				sendMap.clear();
			}else{
				//进入
				fishHttpService.LogIn();
				sendMap.put(1, "#D" + new Random().nextInt(10));				
				mainFrame.updateStackMessage(sendMap);			
			}
		}		
	}
	@Override
	public void displayMessage(String msg, Color color) {
		log.debug(msg);// debug		
		mainFrame.updateDisplayMessage(msg, color);
	}
	@Override
	public void displayMessage(List<Map<String,Object>> listMsg) {
		mainFrame.updateDisplayMessage(listMsg);
	}
	@Override
	public FishThread getFishThread() {
		return fishThread;
	}
	@Override
	public SendMessageThread getSendThread() {
		return sendThread;
	}
	@Override
	public QQThread getQqThread() {
		return qqThread;
	}
	@Override
	public Map<Integer, String> getSendMap() {
		return sendMap;
	}
	@Override
	public void updateLoginInfo(String userId) {
		for (Account account : config.getAccounts()) {
			if (account.getQq().equals(userId)) {
				loginFrame.setUserPwd(account.getProxyPwd());
				loginFrame.setNickName(account.getNickname());
				this.account = account;
				break;
			} else {
				loginFrame.setUserPwd("");
				loginFrame.setNickName("");
			}
		}
	}
}
